package com.vdg.lockphotos;

import java.util.ArrayList;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.actionbarsherlock.app.SherlockActivity;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.vdg.lockphotos.R;
import com.vdg.lockphotos.app.ApplicationManagement;
import com.vdg.lockphotos.data.Photo;
import com.vdg.lockphotos.gridphotos.GridPhotoAdapter;
import com.vdg.lockphotos.notify.OnLoadListImageListener;
import com.vdg.lockphotos.util.Utilities;

public class ListPhotosActivity extends SherlockActivity {
	private GridView mGivPhotos;
	private ArrayList<Photo> mListPhotos = new ArrayList<Photo>();
	private GridPhotoAdapter mGridPhotoAdapter;
	private Handler mHandler = new Handler();
	private RelativeLayout mLnHide;
	private FrameLayout mFrmDialog;
	public static final String ALBUM_URL_KEY = "album_url";
	private String mAlbumUrl;
	private LinearLayout mLnAds;
	private OnLoadListImageListener mOnLoadListImageListener = new OnLoadListImageListener() {

		@Override
		public void onStartLoad() {
			// TODO Auto-generated method stub
			mFrmDialog.setVisibility(View.VISIBLE);
		}

		@Override
		public void onLoadSucessed(ArrayList<Photo> listPhoto) {
			// TODO Auto-generated method stub
			mListPhotos.clear();
			mListPhotos.addAll(listPhoto);
			mGridPhotoAdapter.notifyDataSetChanged();
			mFrmDialog.setVisibility(View.GONE);
		}

		@Override
		public void onLoadFailed() {
			// TODO Auto-generated method stub
			mFrmDialog.setVisibility(View.GONE);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.grid_photos_layout);
		if (getIntent() != null
				&& getIntent().getStringExtra(ALBUM_URL_KEY) != null) {
			mAlbumUrl = getIntent().getStringExtra(ALBUM_URL_KEY);
		}
		init();
		if (mAlbumUrl != null) {
			ApplicationManagement.getInstance(this).loadAllImage(
					mOnLoadListImageListener, mAlbumUrl);
		}
		// loadImage();
		// TODO Auto-generated method stub
	}

	private void updateInGridView(final String url) {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				int start = mGivPhotos.getFirstVisiblePosition();
				for (int i = start, j = mGivPhotos.getLastVisiblePosition(); i <= j; i++) {
					String urlInList = ((Photo) (mGivPhotos
							.getItemAtPosition(i))).getImageUrl();
					if (urlInList != null && urlInList.equals(url)) {
						View view = mGivPhotos.getChildAt(i - start);
						mGivPhotos.getAdapter().getView(i, view, mGivPhotos);
						break;
					}
				}
			}
		});
	}

	private void init() {
		mGivPhotos = (GridView) findViewById(R.id.grd_photos);
		mGridPhotoAdapter = new GridPhotoAdapter(this, 0, mListPhotos);
		mLnHide = (RelativeLayout) findViewById(R.id.btn_hide);
		mLnHide.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mGridPhotoAdapter.getSelectedData() == null
						|| mGridPhotoAdapter.getSelectedData().size() == 0) {
					Utilities.showToast(ListPhotosActivity.this.getResources()
							.getString(R.string.please_select_photo),
							ListPhotosActivity.this);
					return;
				} else {
					if (Utilities.isExternalStorageReadable() == true
							&& Utilities.isExternalStorageWritable() == true) {
						ApplicationManagement.getInstance(
								ListPhotosActivity.this).hidePhotos(
								mGridPhotoAdapter.getSelectedData(), mAlbumUrl);
						mFrmDialog.setVisibility(View.VISIBLE);
					} else {
						Utilities.showToast(
								getResources().getString(
										R.string.sdcard_notready),
								ListPhotosActivity.this);

					}

				}
			}
		});
		mGivPhotos.setAdapter(mGridPhotoAdapter);
		mGivPhotos.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				mListPhotos.get(position).setIsSelected(
						!mListPhotos.get(position).isIsSelected());
				updateInGridView(mListPhotos.get(position).getImageUrl());
			}
		});
		AdView ad = (AdView) findViewById(R.id.adView);
		ad.loadAd(new AdRequest().addTestDevice(getResources().getString(
				R.string.test_devices_id)));
		mFrmDialog = (FrameLayout) findViewById(R.id.frm_dialog);
	}

}
